package ca.llsutherland.nfl.configuration;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;

import ca.llsutherland.nfl.persistence.AbstractDatabaseConnection;

public class PoolConfiguration {

	private Properties properties = null;
    private static final String DB_URL = "db.url";
    private static final String DB_USERNAME = "db.username";
    private static final String DB_PASSWORD = "db.password";
    private static final String DATABASE_CONNECTION = "database.connetion";
    private static final String DB_DRIVER = "db.driver";
	private static PoolConfiguration instance = new PoolConfiguration();
	
	
	public static PoolConfiguration getInstance(){
		return instance;
	}
	
	public PoolConfiguration(){
		this.properties = new Properties();
		loadDefaultProperties();
	}
	
	 private void loadDefaultProperties() {
	        String defaultPropertiesFileName = "FootballPool.properties";
	        if(!loadPropertiesFromResource(defaultPropertiesFileName)){
	            //do nothing, i guess
	        }
	    }
	 
	 private boolean loadPropertiesFromResource(String defaultPropertiesFileName) {
	        boolean resourceLoaded = false;
	        try{
	            InputStream resourceAsStream = PoolConfiguration.class.getResourceAsStream(defaultPropertiesFileName);
	            if(resourceAsStream != null){
	                this.properties.load(resourceAsStream);
	                resourceLoaded = true;
	            }
	        }catch(IOException ex){
	            //huh?
	        }
	        return resourceLoaded;
	    }
	
	public AbstractDatabaseConnection getDatabaseConnection() throws InstantiationException, IllegalAccessException, ClassNotFoundException {
		String databaseConnectionConfigurationSetting = this.properties.getProperty(DATABASE_CONNECTION);
        Object newInstance = Class.forName(databaseConnectionConfigurationSetting).newInstance();
        return (AbstractDatabaseConnection)newInstance;        
    }
	
	public String getDatabaseUrl() {
    	return this.properties.getProperty(DB_URL);
    }
    
    public String getDbUsername() {
    	return this.properties.getProperty(DB_USERNAME);
    }
    
    public String getDbPassword() {
    	return this.properties.getProperty(DB_PASSWORD);
    }
    
    public Properties getConfiguration() {
    	return this.properties;
    }
    
    public void setConfiguration(Properties properties) {
    	this.properties = properties;
    }

    public String getDatabaseDriver() {
        return this.properties.getProperty(DB_DRIVER);
    }

	public List<String> getAllSortedRoles() {
		return getFileNamesFromProperty("roles");
	}
	
	private List<String> getFileNamesFromProperty(String property) {
        String filenames = this.properties.getProperty(property);
        
        String[] rolenames = filenames.split(",");
        if(rolenames != null){
        	return Arrays.asList(rolenames); 
        }else{
        	return new ArrayList<String>();
        }
        
    }
}
